package cn.xdf.docscan

import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.net.Uri
import android.os.Bundle
import android.widget.Button
import com.github.chrisbanes.photoview.PhotoView
import android.widget.ToggleButton
import androidx.activity.ComponentActivity
import cn.xdf.docenhance.Enhancer
import cn.xdf.docenhance.SimpleCpuEngine
import cn.xdf.docenhance.MlkitDocumentEngine
import cn.xdf.docenhance.ShadowRemovalEngine

class EnhanceTestActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_enhance_test)

        val photo = findViewById<PhotoView>(R.id.photo_view)

        val uriStr = intent.getStringExtra("rectified_uri")
        val input = if (uriStr != null) {
            val uri = Uri.parse(uriStr)
            contentResolver.openInputStream(uri)?.use { BitmapFactory.decodeStream(it) }
        } else {
            assets.open("test_doc.jpg").use { BitmapFactory.decodeStream(it) }
        }
        photo.setImageBitmap(input)

        // 默认 CPU 引擎
        var mode = Mode.CPU
        var enableShadowRemoval = false
        var sourceBitmap: Bitmap? = input

        fun buildEnhancer(): Enhancer = Enhancer.Builder(this)
            .setOptions(
                Enhancer.EnhanceOptions(
                    enableTextEnhance = true,
                    textEnhanceLevel = 0.5f,
                    enableLightness = true,
                    lightnessLevel = 0.1f
                )
            )
            .setEngine(
                when (mode) {
                    Mode.ML -> MlkitDocumentEngine(this)
                    else -> SimpleCpuEngine()
                }
            )
            .build()

        fun render() {
            val src = sourceBitmap ?: return
            val bmp = when (mode) {
                Mode.ORIGINAL -> src
                Mode.CPU, Mode.ML -> buildEnhancer().process(src)
            }
            val finalBmp = if (enableShadowRemoval) ShadowRemovalEngine.removeShadow(
                bmp,
                ShadowRemovalEngine.Mode.RGB
            ) else bmp
            photo.setImageBitmap(finalBmp)
        }

        // 初始渲染
        render()

        // 交互控件
        val btnOriginal = findViewById<Button>(R.id.btn_original)
        val btnCpu = findViewById<Button>(R.id.btn_cpu)
        val btnMl = findViewById<Button>(R.id.btn_ml)
        val btnRotate = findViewById<Button>(R.id.btn_rotate)
        val toggleShadow = findViewById<ToggleButton>(R.id.toggle_shadow)

        var rotateDegrees = 0

        fun updateButtons() {
            val isOriginal = mode == Mode.ORIGINAL
            val isCpu = mode == Mode.CPU
            val isMl = mode == Mode.ML
            btnOriginal.alpha = if (isOriginal) 1f else 0.5f
            btnCpu.alpha = if (isCpu) 1f else 0.5f
            btnMl.alpha = if (isMl) 1f else 0.5f
            btnOriginal.isEnabled = !isOriginal
            btnCpu.isEnabled = !isCpu
            btnMl.isEnabled = !isMl
        }

        btnOriginal.setOnClickListener {
            mode = Mode.ORIGINAL
            updateButtons()
            render()
        }
        btnCpu.setOnClickListener {
            mode = Mode.CPU
            updateButtons()
            render()
        }
        btnMl.setOnClickListener {
            mode = Mode.ML
            updateButtons()
            render()
        }
        toggleShadow.setOnCheckedChangeListener { _, isChecked ->
            enableShadowRemoval = isChecked
            render()
        }
        btnRotate.setOnClickListener {
            rotateDegrees = (rotateDegrees + 90) % 360
            photo.rotation = rotateDegrees.toFloat()
        }
        updateButtons()
    }
}


enum class Mode {
    ORIGINAL, // 原图
    CPU, // SimpleCpuEngine 增强
    ML // MlkitDocumentEngine 增强
}

